#include "CallPutPair.h"

#include <stdexcept>

CallPutPair::CallPutPair( const Warrant& call, const Warrant& put )
: m_call( call ),
  m_put( put ),
  m_cost( 0 )
{
    if( call.m_type != Warrant::call )
        throw std::invalid_argument( __FUNCTION__ " argument is not call type" );
    if( put.m_type != Warrant::put )
        throw std::invalid_argument( __FUNCTION__ " argument is not put type" );

    // overall cost
    calculateCost();

}


double CallPutPair::GetCost( )
{
    return m_cost;
}

double CallPutPair::GetRangeStart( )
{
    if( m_cost > 0 )
        return 0;

    return ( m_put.m_strikePrice < m_call.m_strikePrice ) ?
           m_put.m_strikePrice + m_cost : 
           m_call.m_strikePrice + m_cost;
}

double CallPutPair::GetRangeEnd( )
{
    if( m_cost > 0 )
        return 0;

    return ( m_put.m_strikePrice < m_call.m_strikePrice ) ?
           m_call.m_strikePrice - m_cost : 
           m_put.m_strikePrice - m_cost;
}


void CallPutPair::calculateCost()
{
    double call_cost = m_call.m_lastPrice * m_call.m_entRatio;
    double put_cost  = m_put.m_lastPrice * m_put.m_entRatio;

    if( m_put.m_strikePrice < m_call.m_strikePrice )
    {
        m_cost = - call_cost - put_cost;
    }
    else
    {
        m_cost = m_put.m_strikePrice - put_cost 
                    - m_call.m_strikePrice - call_cost;
    }
}